package com.ebelter.nb.ui.activitys;

import android.app.Dialog;
import android.content.DialogInterface;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.ebelter.btcomlib.common.Constants;
import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.StringUtils;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.btcomlib.views.WeightSelectDialog;
import com.ebelter.btcomlib.views.WheelView;
import com.ebelter.btcomlib.views.dialogs.TextTipDialog;
import com.ebelter.btcomlib.views.dialogs.WheelViewDialog;
import com.ebelter.btcomlib.views.dialogs.picktime2.CustomDatePicker;
import com.ebelter.nb.R;
import com.ebelter.nb.constants.NbConstants;
import com.ebelter.nb.model.http.HttpResponse2;
import com.ebelter.nb.model.http.request.NbNetUtils;
import com.ebelter.nb.model.http.response.NbBaseResponse;
import com.ebelter.nb.ui.view.CombinView6;
import com.ebelter.nb.utils.NbUtitls;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import static com.ebelter.btcomlib.views.dialogs.TextTipDialog.Gravity_BUTTOM;

public class AddInfoActivity extends NBBaseActivity {


    public static final String TAG = "AddInfoActivity";
    public static final int requestCode = 107;
    private ImageView top_left_iv;
    private TextView title_tv;
    private TextView rightTitle_tv;
    private RadioGroup rg;
    private RadioGroup rg2;

    private CombinView6 bir_cb6;
    private CombinView6 hei_cb6;
    private CombinView6 wei_cb6;

    private int sex;
    private int role;

    String birthday = "1990-06-15";
    private int height = 170;

    @Override
    protected int loadLayoutById() {
        return R.layout.activity_add_info;
    }

    @Override
    protected void initView() {
        super.initView();
        FV();
        setlistener();

        weightValue2 = UserSpUtil.readFloat(NbConstants.User.weight);
        if (weightValue2 == 0) {
            weightValue2 = 60;
        }
        wei_cb6.setTitle1(StringUtils.getResStr(R.string.tizhong));
        wei_cb6.setTitle2(weightValue2 + " Kg");
    }

    private void setlistener() {
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_r1_addinfo://男
                        sex = 0;
                        rg.setBackgroundResource(R.drawable.bg_role_worker);
                        break;
                    case R.id.rb_r2_addinfo://nv
                        sex = 1;
                        rg.setBackgroundResource(R.drawable.bg_role_athlete);
                        break;
                }
            }
        });
        rg2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb2_r1_addinfo://普通
                        role = 0;
                        rg2.setBackgroundResource(R.drawable.bg_role_worker);
                        break;
                    case R.id.rb2_r2_addinfo://运动员
                        role = 1;
                        rg2.setBackgroundResource(R.drawable.bg_role_athlete);
                        break;
                }
            }
        });

        bir_cb6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showBirDialog();
            }
        });
        hei_cb6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showHeightDialog();
            }
        });
        wei_cb6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showWeightDialog();
            }
        });
        top_left_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }


    private float weightValue2;

    private void showWeightDialog() {


        wei_cb6.setImage1(R.drawable.ic_shangla);


        WeightSelectDialog weightSelectDialog = new WeightSelectDialog(this);
        weightSelectDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                wei_cb6.setImage1(R.drawable.ic_xiala);
            }
        });
//        float weightValue=UserSpUtil.readFloat(NbConstants.User.weight);
//        if (weightValue==0){
//            weightValue=60;
//        }
//        weightValue2=weightValue;
        weightSelectDialog.show(weightValue2);
        weightSelectDialog.setValueChange(new WeightSelectDialog.ValueChange() {
            @Override
            public void change(float value) {
                LogUtils.i(TAG, "--weightSelectDialog--setValueChange--value = " + value);
                weightValue2 = value;
            }
        });

        weightSelectDialog.setBackgroundColor(getResources().getColor(R.color.themeColor));
        weightSelectDialog.setListener(new TextTipDialog.ICancelOkBtListener() {
            @Override
            public void cancel(Dialog dialogView) {
            }//内部已经处理了

            @Override
            public void ok(Dialog dialogView) {
                if (wei_cb6 != null) {
                    wei_cb6.setTitle2(weightValue2 + " Kg");
                }
            }
        });

    }

    private void showBirDialog() {

        bir_cb6.setImage1(R.drawable.ic_shangla);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        String time = sdf.format(new Date());
        String date = time.split(" ")[0];
        CustomDatePicker datePicker = new CustomDatePicker(this, new CustomDatePicker.ResultHandler() {
            @Override
            public void handle(String time) {
                LogUtils.i(TAG, time);
                birthday = time.substring(0, 10);

            }
        }, "1945-01-01 00:00", time);
        // datePicker.setSelectedCalender("1991-06-13");
        datePicker.showSpecificTime(false); //显示时和分
        datePicker.setIsLoop(false);
        datePicker.setDayIsLoop(true);
        datePicker.setMonIsLoop(true);
        datePicker.show(date);

        datePicker.setSelectedTime(birthday);

        datePicker.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                if (bir_cb6 != null) {
                    bir_cb6.setImage1(R.drawable.ic_xiala);
                }
            }
        });
        datePicker.setBackgroundColor(getResources().getColor(R.color.themeColor));
        datePicker.setListener(new TextTipDialog.ICancelOkBtListener() {
            @Override
            public void cancel(Dialog dialogView) {
            }//内部已经处理了

            @Override
            public void ok(Dialog dialogView) {
                bir_cb6.setTitle2(birthday);
            }
        });


    }


    private void showHeightDialog() {
        hei_cb6.setImage1(R.drawable.ic_shangla);
        WheelViewDialog wheelViewDialog = new WheelViewDialog(this);
        wheelViewDialog.setGravity(Gravity_BUTTOM);
        List<String> list = new ArrayList<>();
        for (int i = 100; i < 220; i++) {
            list.add(i + " CM");
        }
        wheelViewDialog.setOffeset(1);
        wheelViewDialog.setItems(list);
        wheelViewDialog.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
                Log.i(TAG, "selectedIndex: " + selectedIndex + ", item: " + item);
                height = NumUtils.string2Int(item.replace(" CM", ""));
            }
        });
        wheelViewDialog.setListener(new TextTipDialog.ICancelOkBtListener() {
            @Override
            public void cancel(Dialog dialogView) {
                dialogView.cancel();
            }

            @Override
            public void ok(Dialog dialogView) {
                dialogView.cancel();
                if (hei_cb6 != null) hei_cb6.setTitle2(height + " CM");
            }
        });
        wheelViewDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                if (hei_cb6 != null) hei_cb6.setImage1(R.drawable.ic_xiala);
            }
        });
        wheelViewDialog.setSelectPosition(height - 100);
        wheelViewDialog.show();
        wheelViewDialog.setBackgroundColor(getResources().getColor(R.color.themeColor));

    }

    private void FV() {
        top_left_iv = findViewById(R.id.top_left_iv);
        title_tv = findViewById(R.id.m_title_tv);
        rightTitle_tv = findViewById(R.id.m_righttitle_tv);
        ViewUtils.setTextViewStr(title_tv, "请填写资料");
        ViewUtils.hideView(rightTitle_tv);

        bir_cb6 = findViewById(R.id.birth_cb6);
        hei_cb6 = findViewById(R.id.height_cb6);
        wei_cb6 = findViewById(R.id.weight_cb6);
        bir_cb6.setWeight(169, 375);
        hei_cb6.setWeight(169, 375);
        wei_cb6.setWeight(169, 375);

        rg = findViewById(R.id.rg_addinfo);
        rg2 = findViewById(R.id.rg2_addinfo);
    }


    //完成
    public void complete(View v) {

        if (!NetUtils.available()) {
            ToastUtil.show(R.string.Network_error);
            return;
        }

        //role=0普通 role=1 运动员
        final int profession = role;
        String dtype = "0";
        final float weight = weightValue2;


        if (weight <= 0) {
            ToastUtil.show("体重必须大于0");
            return;
        }

        final int sex2 = sex;
        final String bir = birthday;
        final int height2 = height;

        if (height2 <= 0) {
            ToastUtil.show("身高必须大于0");
            return;
        }

        //0-非体力劳动者，1-体力劳动者，2-非运动员，3-运动员
        NbNetUtils.getInstance().updateUserInfo(this, NbUtitls.getLoginAuthkey(), null, sex2, bir,
                height2, weight, profession, dtype, new HttpResponse2<NbBaseResponse>() {
                    @Override
                    public void result(boolean isRequestSuccess, String requestResultDesc, int requestResultCode,
                                       NbBaseResponse nbBaseResponse, String s) {
                        LogUtils.i(TAG, s);
                        if (isRequestSuccess && requestResultCode == 1) {
                            //成功
                            ToastUtil.show("设置用户信息成功");
                            saveUserInfo(sex2, bir, height2, profession, weight);
                            setResult(2);
                            finish();
                        } else {
                            //失败
                            ToastUtil.show("设置用户信息失败");
                        }

                    }
                });


    }

    private void saveUserInfo(int sex2, String bir, int height2, int profession, float weight) {

        UserSpUtil.writeString(NbConstants.User.birthday, bir);//yyyy-MM-dd
        UserSpUtil.writeInt(NbConstants.User.height, height2);
        UserSpUtil.writeInt(NbConstants.User.sex, sex2);
        UserSpUtil.writeInt(NbConstants.User.profession, profession);//0普通1运动员
        UserSpUtil.writeFloat(NbConstants.User.weight, weight);

    }
}
